package com.yx.cloud.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.iot.product.sweeper.config.SweeperProductConfig;
import com.iot.sweeper.AppContext;
import com.yx.cloud.R;
import com.yx.cloud.product.ProductConfigConstants;
import com.yx.cloud.utils.DisplayUtil;
import com.yx.cloud.utils.RomUtils;
import com.iot.product.sweeper.bean.CleanSettings;
import com.iot.product.sweeper.bean.Robot;

import org.json.JSONObject;

public class CustomEditBottonDialog extends BottomSheetDialog {
    private static final String TAG = "CustomEditBottonDialog";
    RadioButton iv_pattern01, iv_pattern02, iv_pattern03, iv_pattern04, iv_pattern05,
            iv_water01, iv_water02, iv_water03, iv_water04;
    boolean dustBoxStatus = false;
    private JSONObject productConfig;
    private Context mContext;
    private Button mBtnAlarmGlobalCleanAffirm;
    private RadioGroup mRgPattern, mRgWater, mRgTimes,mBrushSpeed;
    private TextView mTvAlarmPattern, mTvAlarmWater, mTvCleanTimes, mTvFunctionTitle, mTvWaterTitle,mTvBrushSpeed;
    private int mSuction = 0;
    private int mWater = 0;
    private int mTimes = 1;
    private CallBackValue callBackValue = null;
    private Robot robot;

    public CustomEditBottonDialog(Context context, int suction, int water, int times) {
        this(context);
        mSuction = suction;
        mWater = water;
        mTimes = times;
    }

    public CustomEditBottonDialog(Context context) {
        super(context, R.style.LoadProgressDialog);
        robot = AppContext.get().getCurrentDevice();
        productConfig = AppContext.getConfig();
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_edit_botton_dialog);
        showBottomDialog();
    }

    private void showBottomDialog() {
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        mTvFunctionTitle = findViewById(R.id.tv_function_title);
        mTvWaterTitle = findViewById(R.id.tv_water_title);
        mRgPattern = findViewById(R.id.alarmG_radiogroup_pattern);
        mRgWater = findViewById(R.id.alarmG_radiogroup_water);
        mRgTimes = findViewById(R.id.radiogroup_times);
        mBrushSpeed= findViewById(R.id.radiogroup_brush_speed);
        mBtnAlarmGlobalCleanAffirm = findViewById(R.id.btn_alarmGlobalCleanAffirm);

        mTvAlarmPattern = findViewById(R.id.tv_alarmPattern);
        mTvAlarmWater = findViewById(R.id.tv_alarmWater);
        mTvCleanTimes = findViewById(R.id.tv_Times);
        mTvBrushSpeed = findViewById(R.id.tv_brush_speed);
        LinearLayout mLlAlarmGlobalSuction = findViewById(R.id.ll_alarmGlobalSuction);
        LinearLayout mLlAlarmGlobalWater = findViewById(R.id.ll_alarmGlobaWater);
        TextView mTvAlarmGlobalMopHint = findViewById(R.id.tv_alarmGloba_MopHint);

        LinearLayout mllCleanTimes = findViewById(R.id.ll_Times);

        LinearLayout llWorkMode  = findViewById(R.id.ll_custom_cleanMode);//扫拖，只扫，只拖

        iv_pattern01 = findViewById(R.id.iv_pattern01);
        iv_pattern02 = findViewById(R.id.iv_pattern02);
        iv_pattern03 = findViewById(R.id.iv_pattern03);
        iv_pattern04 = findViewById(R.id.iv_pattern04);
        iv_pattern05 = findViewById(R.id.iv_pattern05);

        iv_water01 = findViewById(R.id.iv_water01);
        iv_water02 = findViewById(R.id.iv_water02);
        iv_water03 = findViewById(R.id.iv_water03);
        iv_water04 = findViewById(R.id.iv_water04);
        mLlAlarmGlobalSuction.setVisibility(View.VISIBLE);
        mLlAlarmGlobalWater.setVisibility(View.VISIBLE);
        mllCleanTimes.setVisibility(View.VISIBLE);
        mTvAlarmGlobalMopHint.setVisibility(View.GONE);
        iv_pattern01.setVisibility(View.VISIBLE);


        //不管是否装了拖布都显示水量
        DisplayUtil.show(iv_water01, productConfig.optBoolean(ProductConfigConstants.cleanSetting_hasDryMop));
        DisplayUtil.show(iv_pattern01, productConfig.optBoolean(ProductConfigConstants.cleanSetting_noneSuction));

        if (!productConfig.optBoolean(ProductConfigConstants.cleanSetting_isConbineWaterTankAndDustBox)) { //尘盒水箱分离  只能装其中一个
            CleanSettings settings = robot.getCleanSettings();
            dustBoxStatus = settings.isDustBoxStatus();//尘盒状态
            mTvFunctionTitle.setText(mContext.getString(R.string.U_Suction_Title));
            mTvWaterTitle.setText(mContext.getString(R.string.U_Water_Title));
            if (dustBoxStatus) {
                //只显示吸力
                mTvFunctionTitle.setText(mContext.getString(R.string.U_Suction_Title));
                mTvAlarmGlobalMopHint.setVisibility(View.GONE);
                mLlAlarmGlobalWater.setVisibility(View.GONE);
                iv_pattern01.setVisibility(View.GONE);
            } else {
                mLlAlarmGlobalWater.setVisibility(View.VISIBLE);
                mLlAlarmGlobalSuction.setVisibility(View.GONE);
                iv_water01.setVisibility(View.GONE);

                if(productConfig.optBoolean(ProductConfigConstants.moppingTwoGears)) {
                    mLlAlarmGlobalSuction.setVisibility(View.VISIBLE);
                    iv_pattern01.setVisibility(View.GONE);
                    iv_pattern04.setVisibility(View.GONE);
                    iv_pattern05.setVisibility(View.GONE);
                }else {
                    mLlAlarmGlobalSuction.setVisibility(View.VISIBLE);
                    iv_pattern01.setVisibility(View.GONE);
                }
            }
        }

        if (!productConfig.optBoolean(ProductConfigConstants.custom_water)) {
            mLlAlarmGlobalWater.setVisibility(View.GONE);
        }
        if (!productConfig.optBoolean(ProductConfigConstants.custom_cleanTimes)) {
            mllCleanTimes.setVisibility(View.GONE);
        }

        mRgPattern.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                boolean hasConbineBox=productConfig.optBoolean(ProductConfigConstants.cleanSetting_isConbineWaterTankAndDustBox);
                switch (group.getCheckedRadioButtonId()) {
                    case R.id.iv_pattern01:
                        Log.i(TAG, "iv_pattern01 ------");
                        if (!hasConbineBox && !dustBoxStatus) {
                            mTvAlarmPattern.setText(mContext.getString(R.string.U_Suction_0));
                        }
                        mSuction = 0;
                        break;
                    case R.id.iv_pattern02:
                        Log.i(TAG, "iv_pattern02 ------");
                        if (!hasConbineBox && !dustBoxStatus) {
                            mTvAlarmPattern.setText(mContext.getString(R.string.cleanSet_funState_open));
                        } else {
                            mTvAlarmPattern.setText(mContext.getString(R.string.U_Suction_1));
                        }
                        if(productConfig.optBoolean(ProductConfigConstants.moppingTwoGears)){
                            mTvAlarmPattern.setText(mContext.getString(R.string.U_Suction_1));
                        }
                        mSuction = 1;
                        break;
                    case R.id.iv_pattern03:
                        Log.i(TAG, "iv_pattern03 ------");
                        if (!hasConbineBox && !dustBoxStatus) {
                            mTvAlarmPattern.setText("");
                        } else {
                            mTvAlarmPattern.setText(mContext.getString(R.string.U_Suction_2));
                        }
                        if(productConfig.optBoolean(ProductConfigConstants.moppingTwoGears)){
                            mTvAlarmPattern.setText(mContext.getString(R.string.U_Suction_2));
                        }
                        mSuction = 2;
                        break;
                    case R.id.iv_pattern04:
                        Log.i(TAG, "iv_pattern04 ------");
                        if (!hasConbineBox && !dustBoxStatus) {
                            mTvAlarmPattern.setText("");
                        } else {
                            mTvAlarmPattern.setText(mContext.getString(R.string.U_Suction_3));
                        }
                        mSuction = 3;
                        break;
                    case R.id.iv_pattern05:
                        Log.i(TAG, "iv_pattern05 ------");
                        if (!hasConbineBox && !dustBoxStatus) {
                            mTvAlarmPattern.setText("");
                        } else {
                            mTvAlarmPattern.setText(mContext.getString(R.string.U_Suction_4));
                        }
                        mSuction = 4;
                        break;
                }
            }
        });
        mRgWater.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                boolean hasConbineBox=productConfig.optBoolean(ProductConfigConstants.cleanSetting_isConbineWaterTankAndDustBox);
                switch (group.getCheckedRadioButtonId()) {
                    case R.id.iv_water01:
                        Log.i(TAG, "iv_water01 ------");
                        if (!hasConbineBox && !dustBoxStatus) {
                            mTvAlarmWater.setText("");
                        }
                        mWater = 0;
                        break;
                    case R.id.iv_water02:
                        Log.i(TAG, "iv_water02 ------");
                        mTvAlarmWater.setText(mContext.getString(R.string.U_Water_1));
                        if (!hasConbineBox && !dustBoxStatus) {
                            mTvAlarmWater.setText(mContext.getString(R.string.U_Water_1));
                        }
                        mWater = 1;
                        break;
                    case R.id.iv_water03:
                        Log.i(TAG, "iv_water03 ------");
                        mTvAlarmWater.setText(mContext.getString(R.string.U_Water_2));
                        if (!hasConbineBox && !dustBoxStatus) {
                            mTvAlarmWater.setText(mContext.getString(R.string.U_Water_2));
                        }
                        mWater = 2;
                        break;
                    case R.id.iv_water04:
                        Log.i(TAG, "iv_water04 ------");
                        mTvAlarmWater.setText(mContext.getString(R.string.U_Water_3));
                        if (!hasConbineBox && !dustBoxStatus) {
                            mTvAlarmWater.setText(mContext.getString(R.string.U_Water_3));
                        }
                        mWater = 3;
                        break;
                }
            }
        });

        mRgTimes.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (group.getCheckedRadioButtonId()) {
                    case R.id.rb_once:
                        Log.i(TAG, "iv_once ------");
                        mTvCleanTimes.setText("x1");
                        mTimes = 1;
                        break;
                    case R.id.rb_twice:
                        Log.i(TAG, "iv_twice ------");
                        mTvCleanTimes.setText("x2");
                        mTimes = 2;
                        break;
                    case R.id.rb_three:
                        Log.i(TAG, "iv_three ------");
                        mTvCleanTimes.setText("x3");
                        mTimes = 3;
                        break;
                }
            }
        });

        mBrushSpeed.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (group.getCheckedRadioButtonId()) {
                    case R.id.rb_brush_speed_close:
                        Log.i(TAG, "iv_speed_close ------");
                        mTvBrushSpeed.setText(mContext.getString(R.string.C_Close));
                        break;
                    case R.id.rb_brush_speed_slow:
                        Log.i(TAG, "iv_speed_low ------");
                        mTvBrushSpeed.setText(mContext.getString(R.string.brush_speed_slow));
                        break;
                    case R.id.rb_brush_speed_quickly:
                        Log.i(TAG, "iv_speed_quickly ------");
                        mTvBrushSpeed.setText(mContext.getString(R.string.brush_speed_quickly));
                        break;
                }
            }
        });

        mBtnAlarmGlobalCleanAffirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                boolean hasConbineBox=productConfig.optBoolean(ProductConfigConstants.cleanSetting_isConbineWaterTankAndDustBox);
                if (callBackValue != null) {
                    if (!hasConbineBox && dustBoxStatus) {
                        mWater = -1;
                    }
                    if (!hasConbineBox && !dustBoxStatus) {
                        boolean moppingTwoGears= productConfig.optBoolean(ProductConfigConstants.moppingTwoGears);
                        if(!moppingTwoGears){
                            mSuction = -1;
                        }

                    }
                    callBackValue.SendCleanSetMessage(mSuction, mWater, mTimes);
                }
                dismiss();
            }
        });
        DisplayUtil.show(llWorkMode, productConfig.optBoolean(ProductConfigConstants.carpetSetting_suction));
        //设置透明背景
        getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundResource(android.R.color.transparent);
        initData();
    }

    private void showWorkModeView(int wokeMode){
        if(wokeMode == 0){

        }else if(wokeMode == 1){

        }else if(wokeMode == 2){

        }
    }

    public String getWaterString(int water) {
        String waterStr = null;
        switch (water) {
            case 0:
                waterStr = "sweep";
                break;
            case 1:
                waterStr = "low";
                break;
            case 2:
                waterStr = "mid";
                break;
            case 3:
                waterStr = "high";
                break;
        }
        return waterStr;
    }

    public String getFanString(int fan) {
        String fanStr = null;
        switch (fan) {
            case 0:
                fanStr = "mop";
                break;
            case 1:
                fanStr = "quiet";
                break;
            case 2:
                fanStr = "auto";
                break;
            case 3:
                fanStr = "strong";
                break;
            case 4:
                fanStr = "max";
                break;
        }
        return fanStr;
    }

    @SuppressLint("LongLogTag")
    private void initData() {
        Log.i(TAG, "initData : mSuction " + mSuction + " ; mWater : " + mWater + " ; mTimes : " + mTimes);
        boolean hasConbineBox = productConfig.optBoolean(ProductConfigConstants.cleanSetting_isConbineWaterTankAndDustBox);
        boolean moppingTwoGears = productConfig.optBoolean(ProductConfigConstants.moppingTwoGears);
        //初始化吸力
        if (0 == mSuction) {
            if (!hasConbineBox && !dustBoxStatus) {
                mTvAlarmPattern.setText(mContext.getString(R.string.C_Close));
            }
            iv_pattern01.setChecked(true);
        } else if (1 == mSuction) {
            if (!hasConbineBox && !dustBoxStatus) {
                mTvAlarmPattern.setText(mContext.getString(R.string.cleanSet_funState_open));
            } else {
                mTvAlarmPattern.setText(mContext.getString(R.string.U_Suction_1));
            }
            if(moppingTwoGears){
                mTvAlarmPattern.setText(mContext.getString(R.string.U_Suction_1));
            }
            iv_pattern02.setChecked(true);
        } else if (2 == mSuction) {
            if (!hasConbineBox && !dustBoxStatus) {
                mTvAlarmPattern.setText("");
            } else {
                mTvAlarmPattern.setText(R.string.U_Suction_2);
            }
            if(moppingTwoGears){
                mTvAlarmPattern.setText(R.string.U_Suction_2);
            }
            iv_pattern03.setChecked(true);
        } else if (3 == mSuction) {
            if (!hasConbineBox && !dustBoxStatus) {
                mTvAlarmPattern.setText("");
            } else {
                mTvAlarmPattern.setText(R.string.U_Suction_3);
            }
            iv_pattern04.setChecked(true);
        } else if (4 == mSuction) {
            if (!hasConbineBox && !dustBoxStatus) {
                mTvAlarmPattern.setText("");
            } else {
                mTvAlarmPattern.setText(R.string.U_Suction_4);
            }
            iv_pattern05.setChecked(true);
        } else {
            if (!hasConbineBox) {
                mTvAlarmPattern.setText(R.string.U_Suction_2);
                iv_pattern03.setChecked(true);
            }
        }

        if (!hasConbineBox && dustBoxStatus) {
            return;
        }

        if (0 == mWater) {
            if (!hasConbineBox && !dustBoxStatus) {
                mTvAlarmWater.setText("");
            }
            iv_water01.setChecked(true);
        } else if (1 == mWater) {
            mTvAlarmWater.setText(R.string.U_Water_1);
            iv_water02.setChecked(true);
        } else if (2 == mWater) {
            mTvAlarmWater.setText(R.string.U_Water_2);
            iv_water03.setChecked(true);
        } else if (3 == mWater) {
            mTvAlarmWater.setText(R.string.U_Water_3);
            iv_water04.setChecked(true);
        } else {
            if (!hasConbineBox) {
                mTvAlarmWater.setText(R.string.U_Water_2);
                iv_water03.setChecked(true);
            }
        }

        if (1 == mTimes) {
            mTvCleanTimes.setText("x1");
            mRgTimes.check(R.id.rb_once);
        } else if (2 == mTimes) {
            mTvCleanTimes.setText("x2");
            mRgTimes.check(R.id.rb_twice);
        }

        if(mSuction == 0 && mWater == 0){
            mTvAlarmPattern.setText("");
            mTvAlarmWater.setText("");
        }
    }

    public void setCallBackValue(CallBackValue callBackValue) {
        this.callBackValue = callBackValue;
    }

    //定义一个回调接口
    public interface CallBackValue {
        void SendCleanSetMessage(int suction, int water, int times);
    }
}

